package cn.cqone.crm.service.impl;

import cn.cqone.crm.domain.Role;
import cn.cqone.crm.mapper.PermissionMapper;
import cn.cqone.crm.mapper.RoleMapper;
import cn.cqone.crm.query.RoleQuery;
import cn.cqone.crm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role,Long,RoleQuery> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    //查询全部角色，并给查出每个角色拥有的权限
    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.findAll();
        list.forEach(l -> {
            List<Long> longList = roleMapper.findPerssionIdByRoleId(l.getId());
            longList.forEach( i -> {
                l.getPermissions().add(permissionMapper.findById(i));
            });
        });
        return list;
    }

    //重写删除，在删除角色的同时，去除跟跟角色有关的所有权限
    @Override
    public int delete(Long aLong) {
        permissionMapper.deleteRoleIdPermission(aLong);
        return roleMapper.delete(aLong);
    }
}
